# leetcode 111 二叉树的最小深度

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        def get_height(node):
            # 后序遍历
            if node is None:
                return 0
            left_height = get_height(node.left)
            right_height = get_height(node.right)

            # 左右子树有一个为空，则返回不为空那个子树的高度 + 1
            if node.left is None and node.right:
                return 1 + right_height
            elif node.left and node.right is None:
                return 1 + left_height
            else:
                # 左右子树都不为空，返回高度最小的子树高度 + 1
                return 1 + min(left_height, right_height)
        return get_height(root)
